using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormsApp1
{
    /// <summary>
    /// 显示卡号的自定义对话框
    /// </summary>
    [DesignerCategory("")]
    public class CardNumberDialog : Form
    {
        private Label lblMessage;
        private TextBox txtCardNumber;
        private Button btnOK;
        private Button btnCopy;

        public CardNumberDialog(string cardNumber)
        {
            InitializeComponent(cardNumber);
        }

        private void InitializeComponent(string cardNumber)
        {
            // 窗体设置
            this.Text = "办理成功";
            this.Size = new Size(400, 220);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;

            // 提示信息标签
            lblMessage = new Label
            {
                Text = "办理成功！\n\n您的卡号是：",
                Location = new Point(20, 20),
                Size = new Size(350, 40),
                Font = new Font("微软雅黑", 10F)
            };

            // 卡号文本框（只读，可复制）
            txtCardNumber = new TextBox
            {
                Text = cardNumber,
                Location = new Point(20, 70),
                Size = new Size(340, 30),
                Font = new Font("微软雅黑", 12F, FontStyle.Bold),
                ReadOnly = true,
                TextAlign = HorizontalAlignment.Center,
                BackColor = Color.LightYellow
            };

            // 提示信息标签2
            Label lblTip = new Label
            {
                Text = "请保管好卡号进行预约挂号",
                Location = new Point(20, 110),
                Size = new Size(350, 20),
                Font = new Font("微软雅黑", 9F),
                ForeColor = Color.Gray
            };

            // 复制按钮
            btnCopy = new Button
            {
                Text = "复制卡号",
                Location = new Point(190, 145),
                Size = new Size(80, 30),
                Font = new Font("微软雅黑", 9F)
            };
            btnCopy.Click += BtnCopy_Click;

            // 确定按钮
            btnOK = new Button
            {
                Text = "确定",
                Location = new Point(280, 145),
                Size = new Size(80, 30),
                Font = new Font("微软雅黑", 9F),
                DialogResult = DialogResult.OK
            };

            // 添加控件
            this.Controls.Add(lblMessage);
            this.Controls.Add(txtCardNumber);
            this.Controls.Add(lblTip);
            this.Controls.Add(btnCopy);
            this.Controls.Add(btnOK);

            // 设置默认按钮
            this.AcceptButton = btnOK;

            // 选中卡号文本，方便复制
            txtCardNumber.Select(0, txtCardNumber.Text.Length);
            txtCardNumber.Focus();
        }

        private void BtnCopy_Click(object? sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(txtCardNumber.Text))
                {
                    Clipboard.SetText(txtCardNumber.Text);
                    btnCopy.Text = "已复制√";
                    btnCopy.Enabled = false;
                    
                    // 2秒后恢复按钮状态
                    var timer = new System.Windows.Forms.Timer { Interval = 2000 };
                    timer.Tick += (s, args) =>
                    {
                        btnCopy.Text = "复制卡号";
                        btnCopy.Enabled = true;
                        timer.Stop();
                        timer.Dispose();
                    };
                    timer.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"复制失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

